import { useEffect, useState } from 'react'
import { usePlayVideoStore } from './store'
import { getPercent } from './utils'
import { useMccStore } from '../../store'
import { FetchPlayVideoInfo } from '@/api/manager/playVideo/interface'

export const useData = () => {
	const sliderWidth = 267
	/**是否弹出文件选择弹框 */
	const [isShow, setShow] = useState<boolean>(false)
	const {
		info,
		selectVideoFile,
		setSelectVideoFile,
		fetchInfo,
		setInfo,
		progress,
		onDragProgress,
		setProgress,
		onStartPlay,
		onStopPlay,
		onPausePlay,
		onContinuePlay,
	} = usePlayVideoStore((state) => ({
		info: state.info,
		fetchInfo: state.fetchInfo,
		selectVideoFile: state.selectVideoFile,
		setSelectVideoFile: state.setSelectVideoFile,
		setInfo: state.setInfo,
		progress: state.progress,
		onDragProgress: state.onDragProgress,
		setProgress: state.setProgress,
		onStartPlay: state.onStartPlay,
		onStopPlay: state.onStopPlay,
		onPausePlay: state.onPausePlay,
		onContinuePlay: state.onContinuePlay,
	}))
	useEffect(() => {
		fetchInfo()
	}, [])
	useEffect(() => {
		if (selectVideoFile) {
			//回填放像文件信息
			let _info: FetchPlayVideoInfo = {
				playback_duration: '0',
				file_id: `${selectVideoFile.id}`,
				name: selectVideoFile.file_name,
				vrs_id: selectVideoFile.vrs_id,
				duration: selectVideoFile.duration,
				create_time: selectVideoFile.create_time,
				state: 0,
				dual_stream: 0,
			}
			setInfo(_info)
		}
	}, [selectVideoFile])
	useEffect(() => {
		let _playback_duration = String(parseInt(info?.playback_duration ?? '0'))
		let _duration = info?.duration ?? '0'
		let percent = getPercent(_playback_duration, _duration)
		setProgress(percent)
	}, [info])
	/**恢复放像按钮 */
	const onClickContinuePlay = () => {
		onContinuePlay()
	}
	/**暂停放像按钮 */
	const onClickPausePlay = () => {
		onPausePlay()
	}
	/**点击开始录像按钮 */
	const onClickStartPlay = () => {
		onStartPlay()
	}
	/**停止放像按钮 */
	const onClickStopPlay = () => {
		onStopPlay()
	}
	const handleBack = () => {
		setSelectVideoFile(null)
		useMccStore.getState().setCurPage('mcc_main')
	}
	const handleFile = () => {
		let isShowByInfo = info && info.state !== 0
		if (isShowByInfo) {
			// console.log(t('我是禁用状态'))
			return false
		}
		setShow(true)
	}
	const onClose = () => {
		setShow(false)
	}
	const onOk = () => {
		setShow(false)
	}
	const handleDragProgress = (val: number) => {
		if (info && info.state !== 0) {
			let _duration = info?.duration ?? '0'
			let _playback_duration = String(Math.floor((parseInt(_duration) * val) / 100))
			onDragProgress(_playback_duration)
		}
	}
	const handleChangeProgress = (val: number) => {
		//为了拖拽进度优化
		if (info && info.state !== 0) {
			let _duration = info?.duration ?? '0'
			let _playback_duration = String(Math.floor((parseInt(_duration) * val) / 100))
			setInfo({ ...info, playback_duration: _playback_duration })
		}
	}
	return {
		handleBack,
		isShow,
		handleFile,
		onClose,
		onOk,
		progress,
		info,
		onClickStartPlay,
		sliderWidth,
		setProgress,
		onClickContinuePlay,
		onClickPausePlay,
		onClickStopPlay,
		handleDragProgress,
		handleChangeProgress,
	}
}
